package com.zjd.association.controller;

import com.zjd.association.common.R;
import com.zjd.association.entity.TeamTypes;
import com.zjd.association.service.TeamTypesService;
import com.zjd.association.utils.DateUtils;
import com.zjd.association.utils.UUIDUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;


/**
 * 活动类型 控制层
 *
 * @author: zjd
 * @date: 2024-5-22
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/teamTypes")
public class TeamTypesController {

    private final TeamTypesService teamTypesService;

    /**
     * 获取路径
     *
     * @return 路径
     */
    @RequestMapping("")
    public String index() {
        return "pages/TeamTypes";
    }

    /**
     * 获取活动类型 全部
     *
     * @return 活动类型列表
     */
    @GetMapping("/all")
    public R getAll() {
        List<TeamTypes> list = teamTypesService.getAll();
        return R.ok(list);
    }

    /**
     * 获取活动类型信息
     *
     * @param id 活动类型id
     * @return 活动信息
     */
    @GetMapping("/info")
    public R getInfo(String id) {
        TeamTypes teamTypes = teamTypesService.getOne(id);
        return R.ok(teamTypes);
    }

    /**
     * 获取活动类型信息 分页
     *
     * @param pageIndex 分页页数
     * @param pageSize  分页大学
     * @param teamTypes 分页类型
     * @return 活动类型
     */
    @GetMapping("/page")
    public R getPageInfos(
            Long pageIndex, Long pageSize,
            TeamTypes teamTypes
    ) {
        return R.ok(teamTypesService.getPageInfo(pageIndex, pageSize, teamTypes));
    }

    /**
     * 添加活动类型
     *
     * @param teamTypes 活动类型信息
     * @return 结果
     */
    @PostMapping("/add")
    public R addInfo(TeamTypes teamTypes) {
        teamTypes.setId(UUIDUtils.makeIDByCurrent());
        teamTypes.setCreateTime(DateUtils.getNowDate());
        teamTypesService.add(teamTypes);
        return R.ok();
    }

    /**
     * 更新活动类型
     *
     * @param teamTypes 活动类型信息
     * @return 结果
     */
    @PostMapping("/upd")
    public R updInfo(TeamTypes teamTypes) {
        teamTypesService.update(teamTypes);
        return R.ok();
    }

    /**
     * 删除活动类型
     *
     * @param id 活动id
     * @return 结果
     */
    @PostMapping("/del")
    public R delInfo(String id) {
        if (teamTypesService.isRemove(id)) {
            TeamTypes teamTypes = teamTypesService.getOne(id);
            teamTypesService.delete(teamTypes);
            return R.ok();
        } else {
            return R.warn("存在关联活动，无法移除");
        }
    }

}